package in.equilibr.ingredients.controller;

import in.equilibr.ingredients.dao.ActivityDAO;
import in.equilibr.ingredients.model.Activity;
import in.equilibr.ingredients.service.ActivityService;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping
public class ActivityController {
	private static final Logger logger = LoggerFactory
			.getLogger(ActivityController.class);

	@Autowired
	private ActivityDAO activityDAO;

	@Autowired
	private ActivityService activityService;

	public void init() {

	}

	// get 50 first actions
	@RequestMapping(method = RequestMethod.GET, value = "/activity")
	public ModelAndView showActivities() {
		List<Activity> activities = activityDAO.getActivities(50);
		logger.info("There are " + activities.size() + " activities found.");
		return activityService.buildListView(activities);
	}

	// Neue Aktivitaet anlegen
	@RequestMapping(method = RequestMethod.POST, value = "/activity/add")
	public String addActivity(@ModelAttribute("activity") Activity activity,
			BindingResult result) {
		logger.info("Add new activity " + activity.getName() + ".");
		activityDAO.addActivity(activity);
		return "redirect:/activity/";
	}

	// Delete per URL and via AJAX
	@RequestMapping(method = RequestMethod.GET, value = "/activity/delete/{id}")
	public String deleteActivity(@PathVariable("id") int id) {
		logger.info("Delete record " + id + ".");
		activityDAO.deleteActivityById(id);
		return "redirect:/activity/";
	}

	// Create new Index
	@RequestMapping(method = RequestMethod.GET, value = "/activity/createsearchindex")
	public String createSearchIndex() throws InterruptedException {
		logger.info("Create Lucene-Index.");
		activityDAO.createSearchIndex();
		return "redirect:/activity/";
	}

	// Search function GET
	@RequestMapping(method = RequestMethod.GET, value = "/activity/search")
	public ModelAndView getSearchResult(@RequestParam("term") String term) {
		List<Activity> activities = activityDAO.searchActivity(term);
		logger.info("There are " + activities.size() + " activities found.");

		return activityService.buildListView(activities);
	}

	// Search function for Autocomplete POST
	@RequestMapping(method = RequestMethod.POST, value = "/activity/search")
	public ResponseEntity<String> getAutocompleteResult(
			@RequestParam("term") String term) throws IOException {

		// New Response-Header is necessary, otherwise ISO-coded response
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");

		List<String> activities = activityDAO.searchActivityName(term);

		if (activities.size() == 0) {
			logger.info("No records in Database - try auto correction.");
			activities = activityDAO.searchSimilarActivities(term);
		} else {
			logger.info("There are " + activities.size()
					+ " activities found - Autocomplete.");
		}
		String json = activityService.convertJson(activities);
		return new ResponseEntity<String>(json, responseHeaders,
				HttpStatus.CREATED);
	}
}
